//============================================================================
// Copyright (C) 2009-2013 Brett R. Jones 
// All Rights Reserved
//
// You may redistribute and/or modify for non commercial and commercial uses 
// provided this copyright notice remains in place and is not modified
//
// This code is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
// brettjones1900@gmail.com
// http://www.p2panarchy.com
//============================================================================
 
// example class for pkt handler ease of coding
#include "RcSysPktLib.h"

//=== constructor ===//
RcSysPktHandlerBase::RcSysPktHandlerBase()
{
	for( int i = 0; i <= sizeof( m_aBaseSysPktFuncTable ) / sizeof( void * ); i++ )
	{
		m_aBaseSysPktFuncTable[ i ] = &RcSysPktHandlerBase::OnPktUnhandled;
	}
	m_aBaseSysPktFuncTable[ 0 ] = &RcSysPktHandlerBase::OnPktInvalid;

	m_aBaseSysPktFuncTable[ (RC_SYS_PKT_TYPE_DIE & 0xff) ]					= &RcSysPktHandlerBase::OnPktDie;
	m_aBaseSysPktFuncTable[ (RC_SYS_PKT_TYPE_AUTH & 0xff) ]					= &RcSysPktHandlerBase::OnPktAuth;
	m_aBaseSysPktFuncTable[ (RC_SYS_PKT_TYPE_ANN_LIST & 0xff) ]				= &RcSysPktHandlerBase::OnPktAnnList;
	m_aBaseSysPktFuncTable[ (RC_SYS_PKT_TYPE_ANNOUNCE & 0xff) ]				= &RcSysPktHandlerBase::OnPktAnnounce;
	m_aBaseSysPktFuncTable[ (RC_SYS_PKT_TYPE_FIND_NODE & 0xff) ]			= &RcSysPktHandlerBase::OnPktFindNode;
	m_aBaseSysPktFuncTable[ (RC_SYS_PKT_TYPE_OFFER_SYSTEM_MSG & 0xff) ]		= &RcSysPktHandlerBase::OnPktOfferSystemMsg;
//	m_aBaseSysPktFuncTable[ (RC_SYS_PKT_TYPE_OFFER_STATUS & 0xff) ]			= &RcSysPktHandlerBase::PktOfferStatus;
	m_aBaseSysPktFuncTable[ (RC_SYS_PKT_TYPE_ANN_APP_UPDATE & 0xff) ]		= &RcSysPktHandlerBase::OnPktAnnTronacomUpdate;
	m_aBaseSysPktFuncTable[ (RC_SYS_PKT_TYPE_PATCH & 0xff) ]				= &RcSysPktHandlerBase::OnPktPatch;
	m_aBaseSysPktFuncTable[ (RC_SYS_PKT_TYPE_PING & 0xff) ]					= &RcSysPktHandlerBase::OnPktPing;
	m_aBaseSysPktFuncTable[ (RC_SYS_PKT_TYPE_PONG & 0xff) ]					= &RcSysPktHandlerBase::OnPktPong;

	m_aBaseSysPktFuncTable[ (RC_SYS_PKT_TYPE_PROXY_SERVICE_REQ & 0xff) ]	= &RcSysPktHandlerBase::OnPktRelayServiceReq;
	m_aBaseSysPktFuncTable[ (RC_SYS_PKT_TYPE_PROXY_SERVICE_REPLY & 0xff) ]	= &RcSysPktHandlerBase::OnPktRelayServiceReply;
	m_aBaseSysPktFuncTable[ (RC_SYS_PKT_TYPE_PROXY_CONNECT_REQ & 0xff) ]	= &RcSysPktHandlerBase::OnPktRelayConnectReq;
	m_aBaseSysPktFuncTable[ (RC_SYS_PKT_TYPE_PROXY_CONNECT_REPLY & 0xff) ]	= &RcSysPktHandlerBase::OnPktRelayConnectReply;
	m_aBaseSysPktFuncTable[ (RC_SYS_PKT_TYPE_PROXY_USER_DISCONNECT & 0xff) ]= &RcSysPktHandlerBase::OnPktRelayUserDisconnect;

	m_aBaseSysPktFuncTable[ (RC_SYS_PKT_TYPE_SEARCH_REQ & 0xff) ]			= &RcSysPktHandlerBase::OnPktSearchReq;
	m_aBaseSysPktFuncTable[ (RC_SYS_PKT_TYPE_SEARCH_REPLY & 0xff) ]			= &RcSysPktHandlerBase::OnPktSearchReply;
	m_aBaseSysPktFuncTable[ (RC_SYS_PKT_TYPE_FIND_FILE_REQ & 0xff) ]		= &RcSysPktHandlerBase::OnPktFindFileReq;
	m_aBaseSysPktFuncTable[ (RC_SYS_PKT_TYPE_FIND_FILE_REPLY & 0xff) ]		= &RcSysPktHandlerBase::OnPktFindFileReply;


}
//=== destructor ===//
RcSysPktHandlerBase::~RcSysPktHandlerBase()
{
}
//---------------------------------------------------------------------------
//! Handle Incomming packet.. use function jump table for speed
long RcSysPktHandlerBase::RcSysHandlePkt( RcSktBase * poSkt, RcSysPktHdr * poPktHdr )
{
	if( poPktHdr->m_u16PktType <= sizeof(  m_aBaseSysPktFuncTable ) / sizeof( void *) )
		return (this->*m_aBaseSysPktFuncTable[ poPktHdr->m_u16PktType ])(poSkt, poPktHdr);
	return OnPktInvalid(poSkt, poPktHdr);
}

/*

//! override this to handle status messages
void RcSysPktHandlerBase::PktStatusMsg( char * pMsg, ... )
{
	return;
}
//! override this to handle error messages
void RcSysPktHandlerBase::PktErrorMsg( char * pMsg, ... )
{
	return;
}

//! give apps chance to override and place callers user data in thier own structure
long RcSysPktHandlerBase::RcSktSetUserData		( RcSktBase * poSkt )
{
	return 0;
}
//! give apps chance to override and return callers user data from within thier own structure
long RcSysPktHandlerBase::RcSktGetUserData( U64 u64SktNum, void ** pvRetUserData )
{
	return 0;
}


//=== packet handler methods ===//
//! Handle Incomming packet.. use function jump table for speed
long RcSysPktHandlerBase::RcHandlePkt( RcSktBase * poSkt, RcSysPktHdr * poPktHdr )
{
	return 0;
};
//! override this function with your own send packet
long RcSysPktHandlerBase::RcSendPkt( U64 u64SktNum, RcSysPktHdr * poPktHdr, BOOL bDisconnect )
{
	return 0;
}
//! override this function for your own connect to 
long RcSysPktHandlerBase::RcConnectTo		( RcContactIdent * poIdent, void * pvUserData, U64 * pu64RetSktNum )
{
	return 0;
}
//! override this function to close socket connection
long RcSysPktHandlerBase::RcCloseConnection	( U64 u64SktNum )
{
	return 0;
}
*/


//=== packet handlers ===//
//============================================================================
long RcSysPktHandlerBase::OnPktUnhandled( RcSktBase * poSkt, RcSysPktHdr * poPktHdr )
{
	return 0;
}
//============================================================================
long RcSysPktHandlerBase::OnPktInvalid( RcSktBase * poSkt, RcSysPktHdr * poPktHdr )
{
	return 0;
}


//============================================================================
long RcSysPktHandlerBase::OnPktDie( RcSktBase * poSkt, RcSysPktHdr * poPktHdr )
{
	return 0;
}
//============================================================================
long RcSysPktHandlerBase::OnPktAuth( RcSktBase * poSkt, RcSysPktHdr * poPktHdr )
{
	return 0;
}
//============================================================================
long RcSysPktHandlerBase::OnPktAnnList( RcSktBase * poSkt, RcSysPktHdr * poPktHdr )
{
	return 0;
}
//============================================================================
long RcSysPktHandlerBase::OnPktAnnounce( RcSktBase * poSkt, RcSysPktHdr * poPktHdr )
{
	return 0;
}
//============================================================================
long RcSysPktHandlerBase::OnPktFindNode( RcSktBase * poSkt, RcSysPktHdr * poPktHdr )
{
	return 0;
}
//============================================================================
long RcSysPktHandlerBase::OnPktFindFileReq( RcSktBase * poSkt, RcSysPktHdr * poPktHdr )
{
	return 0;
}
//============================================================================
long RcSysPktHandlerBase::OnPktFindFileReply( RcSktBase * poSkt, RcSysPktHdr * poPktHdr )
{
	vx_assert( FALSE );
	return 0;
}

//============================================================================
long RcSysPktHandlerBase::OnPktOfferSystemMsg( RcSktBase * poSkt, RcSysPktHdr * poPktHdr )
{
	vx_assert( FALSE );
	return 0;
}
//============================================================================
long RcSysPktHandlerBase::OnPktAnnTronacomUpdate( RcSktBase * poSkt, RcSysPktHdr * poPktHdr )
{
	vx_assert( FALSE );
	return 0;
}
//============================================================================
long RcSysPktHandlerBase::OnPktPatch( RcSktBase * poSkt, RcSysPktHdr * poPktHdr )
{
	vx_assert( FALSE );
	return 0;
}
//============================================================================
long RcSysPktHandlerBase::OnPktPing( RcSktBase * poSkt, RcSysPktHdr * poPktHdr )
{
	vx_assert( FALSE );
	return 0;
}
//============================================================================
long RcSysPktHandlerBase::OnPktPong( RcSktBase * poSkt, RcSysPktHdr * poPktHdr )
{
	vx_assert( FALSE );
	return 0;
}
//============================================================================
long RcSysPktHandlerBase::OnPktRelayServiceReq( RcSktBase * poSkt, RcSysPktHdr * poPktHdr )
{
	return 0;
}
//============================================================================
long RcSysPktHandlerBase::OnPktRelayServiceReply( RcSktBase * poSkt, RcSysPktHdr * poPktHdr )
{
	return 0;
}
//============================================================================
long RcSysPktHandlerBase::OnPktRelayConnectReq( RcSktBase * poSkt, RcSysPktHdr * poPktHdr )
{
	return 0;
}
//============================================================================
long RcSysPktHandlerBase::OnPktRelayConnectReply( RcSktBase * poSkt, RcSysPktHdr * poPktHdr )
{
	return 0;
}
//============================================================================
long RcSysPktHandlerBase::OnPktRelayUserDisconnect( RcSktBase * poSkt, RcSysPktHdr * poPktHdr )
{
	return 0;
}
//============================================================================
long RcSysPktHandlerBase::OnPktSearchReq( RcSktBase * poSkt, RcSysPktHdr * poPktHdr )
{
	return 0;
}
//============================================================================
long RcSysPktHandlerBase::OnPktSearchReply( RcSktBase * poSkt, RcSysPktHdr * poPktHdr )
{
	return 0;
}
